import { storage } from '@/tools/storage';
import qs from 'qs';
import { useYqappStore } from '@/stores/yqapp';

// #ifdef H5
import smCrypto from 'sm-crypto';
// #endif

// #ifdef MP-WEIXIN
import smCrypto from 'miniprogram-sm-crypto';
// #endif

// #ifndef MP-ALIPAY
import JSEncrypt from 'jsencrypt'
// #endif

import { AES, mode, pad, enc, SHA256, MD5 } from 'crypto-js'

// rsa公钥
const rsaPublicKey = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuaLIRC+ao4P2xuNZ6xuezD3pwu/iyarFySaAkj7gEKW9EPYCErVdF42L6Ns1X8iwZlOSDEIjIMddm+t4Zj3Nn/BfY36cuFCECttYUje5SzLYOjoAM2JeKryJFhm9sJPhFNNNa78SfFk7mOkjSm6dwaa6f5uCdkkLYv+hw+DZxFhwhPpkwSRkuJ0Hkgb+caUuJG4suLZFLgV7HvPu3JhXh0ayW1+gRl98b525j5zherqgWBgT3CZZTzp3KoWGFP0h1Saa/jk8Wm/xTtLdHfmTipFGJNJjBvPK8Ksv+4YoDWzeMToEdmLSPP/Y3Z9VaRVEBfkf2l2RMTtb89+05BKCaQIDAQAB';
// rsa私钥
const rsaPrivateKey = 'MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC5oshEL5qjg/bG41nrG57MPenC7+LJqsXJJoCSPuAQpb0Q9gIStV0XjYvo2zVfyLBmU5IMQiMgx12b63hmPc2f8F9jfpy4UIQK21hSN7lLMtg6OgAzYl4qvIkWGb2wk+EU001rvxJ8WTuY6SNKbp3Bprp/m4J2SQti/6HD4NnEWHCE+mTBJGS4nQeSBv5xpS4kbiy4tkUuBXse8+7cmFeHRrJbX6BGX3xvnbmPnOF6uqBYGBPcJllPOncqhYYU/SHVJpr+OTxab/FO0t0d+ZOKkUYk0mMG88rwqy/7higNbN4xOgR2YtI8/9jdn1VpFUQF+R/aXZExO1vz37TkEoJpAgMBAAECggEAYeDVjTY4Z/z9khJv58oRaYk/h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vw0I6xlrPyq4euHW9dccWSLzjYYlpZ20Lh8DGMlrum2FOQyEaCw7eMdVW/lzDQq2qLuZsjGLbf0rBIHDL10t3aCuh7uJSUAx96aPw=';
// rsa 实例
const rsa = new JSEncrypt();
rsa.setPublicKey(rsaPublicKey);
rsa.setPrivateKey(rsaPrivateKey)

// aes 密钥
const aesDefaultKey = import.meta.env.VITE_AESDEFAULTKEY || 'CSqGSIb3DQEBAQUAA4GNADCBiQKBgQC3hU1Q9vH'

export default {
    // #ifdef MP-ALIPAY
    // rsa 加密
    async rsaEncrypt(str) {
        return new Promise(async resolve => {
            my.rsa({
                action: 'encrypt',
                text: str,
                key: rsaPublicKey,
                success: result => resolve(result.text),
                fail: () => resolve(''),
            })
        })
    },
    // #endif
    // #ifndef MP-ALIPAY
    // rsa 加密
    rsaEncrypt(str) {
        return rsa.encrypt(str)
    },
    // rsa 解密
    rsaDecrypt(str) {
        return rsa.decrypt(str)
    },
    // #endif

    // aes 加密（CBC 模式）
    aesEncrypt(str, aesKey, modeStr = 'CBC') {
        return AES.encrypt(str, aesKey || aesKey === 0 ? aesKey : aesDefaultKey, {
            mode: mode[modeStr],
            padding: pad.Pkcs7,
        }).toString()
    },
    // aes 加密（ECB 模式）
    aesEncryptByModeEcb(str, aesKey, modeStr = 'ECB') {
        str = enc.Utf8.parse(str)
        aesKey = enc.Utf8.parse(aesKey)
        return this.aesEncrypt(str, aesKey, modeStr)
    },
    // aes 解密
    aesDecrypt(str, aesKey, modeStr = 'CBC') {
        if (!str) {
            return
        }
        return AES.decrypt(str, aesKey || aesKey === 0 ? aesKey : aesDefaultKey, {
            mode: mode[modeStr],
            padding: pad.Pkcs7,
        }).toString(enc.Utf8)
    },
    // md5 摘要
    md5Encrypt(str) {
        return MD5(str).toString()
    },
    // sha256 摘要
    sha256Encrypt(str) {
        return SHA256(str).toString()
    },
};
